package com.example.android.commitcontent.ime.database.hanzi;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.android.commitcontent.ime.beans.HanZiCharacter;
import com.example.android.commitcontent.ime.beans.HanZiWord;

import java.util.ArrayList;
import java.util.List;

public class HanZiStatisticsDataBaseHelper {

    private Context mContext;
    private static HanziStatisticsDatabase hanziDatabase;

    public HanZiStatisticsDataBaseHelper(Context context) {
        mContext = context;
    }

    public static HanZiStatisticsDataBaseHelper intance(Context context) {
        if (mHanziHelper == null) {
            mHanziHelper = new HanZiStatisticsDataBaseHelper(context);
            hanziDatabase = new HanziStatisticsDatabase(context);
        }
        return mHanziHelper;
    }

    private static HanZiStatisticsDataBaseHelper mHanziHelper;


    /**
     * 字频统计
     *
     * @param hanZiCharacter
     */
    public void repeatCount(HanZiCharacter hanZiCharacter) {
        SQLiteDatabase writableDatabase = hanziDatabase.getWritableDatabase();
        Cursor cursor = writableDatabase.query(HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME,
                null, String.format("%s = ?", HanZiStatisticsTable.HanZiLetterTable.Columns.HANZI), new String[]{hanZiCharacter.hanzi},
                null, null, null);
        if (cursor.moveToNext()) {
            int num = cursor.getInt(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.REPEAT_COUNT));
            num++;
            ContentValues values = new ContentValues();
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.REPEAT_COUNT, num);
            writableDatabase.update(HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME, values, String.format("%s=?", HanZiStatisticsTable.HanZiLetterTable.Columns.HANZI), new String[]{hanZiCharacter.hanzi});
        } else {
            ContentValues values = new ContentValues();
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.HANZI, hanZiCharacter.hanzi);
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.SHENG_MU, hanZiCharacter.shengmu);
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.YUN_MU, hanZiCharacter.yunmu);
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.PIN_YIN, hanZiCharacter.pinyin);
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.BI_HUA, hanZiCharacter.bihua);
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.BI_SHUN, hanZiCharacter.bishun);
            values.put(HanZiStatisticsTable.HanZiLetterTable.Columns.REPEAT_COUNT, 1);
            writableDatabase.insert(HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME, null, values);
        }
        cursor.close();
        writableDatabase.close();
    }

    public List<HanZiCharacter> getSavedHanZi() {
        List<HanZiCharacter> hanZiCharacters = new ArrayList<>();
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiTable.TABLE_NAME,
                null, null, null,
                null, null, null);
        while (cursor.moveToNext()) {
            String hanzi = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiTable.Columns.HANZI));
            String shengmu = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiTable.Columns.SHENG_MU));
            String yunmu = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiTable.Columns.YUN_MU));
            String pinyin = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiTable.Columns.PIN_YIN));
            int bihua = cursor.getInt(cursor.getColumnIndex(HanZiStatisticsTable.HanZiTable.Columns.BI_HUA));
            String bishun = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiTable.Columns.BI_SHUN));
            hanZiCharacters.add(new HanZiCharacter(hanzi, shengmu, yunmu, pinyin, bihua, bishun));
        }
        cursor.close();
        readableDatabase.close();
        return hanZiCharacters;
    }

    public void saveHanZi(List<HanZiCharacter> hanZiCharacterList) {
        SQLiteDatabase writableDatabase = hanziDatabase.getWritableDatabase();
        writableDatabase.execSQL("delete from " + HanZiStatisticsTable.HanZiTable.TABLE_NAME);
        for (HanZiCharacter hanZiCharacter : hanZiCharacterList) {
            ContentValues values = new ContentValues();
            values.put(HanZiStatisticsTable.HanZiTable.Columns.HANZI, hanZiCharacter.hanzi);
            values.put(HanZiStatisticsTable.HanZiTable.Columns.SHENG_MU, hanZiCharacter.shengmu);
            values.put(HanZiStatisticsTable.HanZiTable.Columns.YUN_MU, hanZiCharacter.yunmu);
            values.put(HanZiStatisticsTable.HanZiTable.Columns.PIN_YIN, hanZiCharacter.pinyin);
            values.put(HanZiStatisticsTable.HanZiTable.Columns.BI_HUA, hanZiCharacter.bihua);
            values.put(HanZiStatisticsTable.HanZiTable.Columns.BI_SHUN, hanZiCharacter.bishun);
            writableDatabase.insert(HanZiStatisticsTable.HanZiTable.TABLE_NAME, null, values);
        }
        writableDatabase.close();
    }

    public void wordsRepeatCount(HanZiWord hanZiWord) {
        wordsRepeatCount(hanZiWord, 1, false);
    }

    public void wordsRepeatCount(HanZiWord hanZiWord, int count, boolean isOverrideMode) {
        SQLiteDatabase writableDatabase = hanziDatabase.getWritableDatabase();
        Cursor cursor = writableDatabase.query(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME,
                null, String.format("%s = ?", HanZiStatisticsTable.HanZiWordsTable.Columns.HANZI_WORD), new String[]{hanZiWord.hanziWord},
                null, null, null);
        if (cursor.moveToNext()) {
            int num = cursor.getInt(cursor.getColumnIndex(HanZiStatisticsTable.HanZiWordsTable.Columns.REPEAT_COUNT));
            if (isOverrideMode)
                num = hanZiWord.repeatCount;
            else
                num += count;
            ContentValues values = new ContentValues();
            values.put(HanZiStatisticsTable.HanZiWordsTable.Columns.REPEAT_COUNT, num);
            writableDatabase.update(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME, values, String.format("%s=?", HanZiStatisticsTable.HanZiWordsTable.Columns.HANZI_WORD), new String[]{hanZiWord.hanziWord});
        } else {
            ContentValues values = new ContentValues();
            values.put(HanZiStatisticsTable.HanZiWordsTable.Columns.HANZI_WORD, hanZiWord.hanziWord);
            values.put(HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN, hanZiWord.pinyin);
            values.put(HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN_SIMPLIFIED, hanZiWord.pinyinSimplified);
            values.put(HanZiStatisticsTable.HanZiWordsTable.Columns.REPEAT_COUNT, isOverrideMode ? hanZiWord.repeatCount : count);
            writableDatabase.insert(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME, null, values);
        }
        cursor.close();
        writableDatabase.close();
    }

    public int getWordsRepeatCount(String hanzi) {
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME,
                null, String.format("%s=?", HanZiStatisticsTable.HanZiWordsTable.Columns.HANZI_WORD), new String[]{hanzi},
                null, null, null);
        if (cursor.moveToNext()) {
            return getWord(cursor).repeatCount;
        }
        cursor.close();
        readableDatabase.close();
        return 0;
    }

    /**
     * 获取字频
     *
     * @param hanzi
     * @return
     */
    public int getRepeatCount(String hanzi) {
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME,
                null, String.format("%s=?", HanZiStatisticsTable.HanZiLetterTable.Columns.HANZI), new String[]{hanzi},
                null, null, null);
        if (cursor.moveToNext()) {
            return get(cursor).repeatCount;
        }
        cursor.close();
        readableDatabase.close();
        return 0;
    }

    public List<HanZiCharacter> findHaZiByShengMu(String shengmu) {
        List<HanZiCharacter> hanZiCharacters = new ArrayList<>();
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
//        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME,
//                null, String.format("%s like '?%%'", HanZiStatisticsTable.HanZiLetterTable.Columns.PIN_YIN), new String[]{shengmu},
//                null, null, null);
        String format = String.format("select * from %s where %s like '%s%%'", HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME, HanZiStatisticsTable.HanZiLetterTable.Columns.PIN_YIN, shengmu);
        Cursor cursor = readableDatabase.rawQuery(format, null);
        while (cursor.moveToNext()) {
            hanZiCharacters.add(get(cursor));
        }
        cursor.close();
        readableDatabase.close();
        return hanZiCharacters;
    }

    /**
     * 获取所有统计的汉字列表
     *
     * @return
     */
    public List<HanZiCharacter> getHanZiList() {
        List<HanZiCharacter> hanZiCharacters = new ArrayList<>();
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiLetterTable.TABLE_NAME,
                null, null, null,
                null, null, null);
        while (cursor.moveToNext()) {
            hanZiCharacters.add(get(cursor));
        }
        cursor.close();
        readableDatabase.close();
        return hanZiCharacters;
    }

    public List<HanZiWord> getHanZiWordList() {
        List<HanZiWord> hanZiWords = new ArrayList<>();
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME,
                null, null, null,
                null, null, null);
        while (cursor.moveToNext()) {
            hanZiWords.add(getWord(cursor));
        }
        cursor.close();
        readableDatabase.close();
        return hanZiWords;
    }

    private HanZiCharacter get(Cursor cursor) {
        String hanzi = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.HANZI));
        String shengmu = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.SHENG_MU));
        String yunmu = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.YUN_MU));
        String pinyin = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.PIN_YIN));
        int bihua = cursor.getInt(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.BI_HUA));
        String bishun = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.BI_SHUN));
        int repeatCount = cursor.getInt(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.REPEAT_COUNT));
        return new HanZiCharacter(hanzi, shengmu, yunmu, pinyin, bihua, bishun, repeatCount);
    }

    private HanZiWord getWord(Cursor cursor) {
        String hanzi = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiWordsTable.Columns.HANZI_WORD));
        String pinyin = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN));
        String pinyin_simplified = cursor.getString(cursor.getColumnIndex(HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN_SIMPLIFIED));
        int repeatCount = cursor.getInt(cursor.getColumnIndex(HanZiStatisticsTable.HanZiLetterTable.Columns.REPEAT_COUNT));
        return new HanZiWord(hanzi, pinyin, pinyin_simplified, repeatCount);
    }

    public List<HanZiCharacter> queryWordListByPinyin(String pinyin) {
        List<HanZiCharacter> hanZiCharacters = new ArrayList<>();
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME,
                null, String.format("%s=?", HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN), new String[]{pinyin},
                null, null, null);
        while (cursor.moveToNext()) {
            HanZiWord word = getWord(cursor);
            hanZiCharacters.add(new HanZiCharacter(word.hanziWord, word.pinyin, word.repeatCount));
        }
        cursor.close();
        readableDatabase.close();
        return hanZiCharacters;
    }

    public List<HanZiCharacter> queryWordListByPinyinSimplified(String pinyin) {
        List<HanZiCharacter> hanZiCharacters = new ArrayList<>();
        String[] split = pinyin.split(",");
        pinyin = "";
        for (int i = 0; i < split.length; i++) {
            pinyin += split[i];
            pinyin += "%";
        }
        SQLiteDatabase readableDatabase = hanziDatabase.getReadableDatabase();
//        Cursor cursor = readableDatabase.query(HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME,
//                null, String.format("%s=?", HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN_SIMPLIFIED), new String[]{pinyin},
//                null, null, null);
        String format = String.format("select * from %s where %s like '%s'", HanZiStatisticsTable.HanZiWordsTable.TABLE_NAME, HanZiStatisticsTable.HanZiWordsTable.Columns.PIN_YIN_SIMPLIFIED, pinyin);
        Cursor cursor = readableDatabase.rawQuery(format, null);
        while (cursor.moveToNext()) {
            HanZiWord word = getWord(cursor);
            hanZiCharacters.add(new HanZiCharacter(word.hanziWord, word.pinyin, word.repeatCount));
        }
        cursor.close();
        readableDatabase.close();
        return hanZiCharacters;
    }
}
